package com.ftar.urgent.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.ftar.urgent.R;
import com.ftar.urgent.databinding.ActivityBaseBinding;
import com.ftar.urgent.viewmodel.CommonEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * @author: wei.jin
 * @date: 2023/7/7
 * @email: wei.jin@fanttec.com
 * @description：
 */
public abstract class BaseActivity extends AppCompatActivity {
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.CAMERA",
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE",
            "android.permission.MANAGE_EXTERNAL_STORAGE",
            "android.permission.RECORD_AUDIO",
            "android.permission.READ_PHONE_STATE",
    };
    protected ActivityBaseBinding mRootBind;
    private boolean mActivityVisible = true;

    /**
     * Get child binding
     * @return
     */
    protected abstract ViewDataBinding getChildBinding();

    /**
     * Get voice hint in the bottom
     * @return
     */
    protected abstract String voiceHint();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        verifyStoragePermissions(this);
        EventBus.getDefault().register(this);

        mRootBind = DataBindingUtil.setContentView(this, R.layout.activity_base);
        mRootBind.content.addView(getChildBinding().getRoot());

        String hint = voiceHint();
        if(hint == null) {
            mRootBind.bottomLayout.setVisibility(View.GONE);
        }else {
            mRootBind.bottomLayout.setVisibility(View.VISIBLE);
            mRootBind.voiceHint.setText(voiceHint());
        }
    }

    protected void setVoiceHint(){
        mRootBind.voiceHint.setText(voiceHint());
    }

    @Override
    protected void onResume() {
        super.onResume();
        mActivityVisible = true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        mActivityVisible = false;
    }

    /**
     * Activity is visible
     * @return
     */
    public boolean isActivityVisible() {
        return mActivityVisible;
    }

    public static void verifyStoragePermissions(Activity activity) {
        try {
            //检测是否有写的权限
            int permission = ActivityCompat.checkSelfPermission(activity,
                    "android.permission.WRITE_EXTERNAL_STORAGE");
            if (permission != PackageManager.PERMISSION_GRANTED) {
                // 没有写的权限，去申请写的权限，会弹出对话框
                ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCommonEvent(CommonEvent event) {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    protected void backDeskTop(){
        Intent intent = new Intent(this,MainActivity.class);
        intent.putExtra(MainActivity.TAG_EXIT, true);
        startActivity(intent);
    }

}